package net.kleinhenz.norabase.client;

import net.kleinhenz.norabase.client.datasource.BattlegroupListDS;

import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.DoubleClickEvent;
import com.smartgwt.client.widgets.events.DoubleClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.SelectionStyle;

public class BattlegroupOpenDialog extends Window {

	public BattlegroupOpenDialog(final BattlegroupTabSet bgts) {
		
		setSize("700px", "366px");
		setIsModal(true);
		setShowBody(true);
		setShowFooter(false);
		setShowHeaderBackground(false);
		setShowHeaderIcon(false);
		setShowMinimizeButton(false);
		setShowStatusBar(true);
		setShowTitle(true);
		setTitle("Open Battlegroup");
		centerInPage();
		setAutoSize(true);  
        setCanDragReposition(true);  
        setCanDragResize(false);  
        setMargin(10);
        
        VLayout outerLayout = new VLayout();
        outerLayout.setSize("100%", "100%");
        outerLayout.setMargin(10);
        outerLayout.setMembersMargin(10);
        
        final TabSet tabSet = new TabSet();
        tabSet.setSize("100%", "500px");
        
        final Tab myBattlegroupsTab = new Tab("My Battlegroups");
        
        VLayout myBGlayout = new VLayout();
        myBGlayout.setMembersMargin(10);
        
        final ListGrid myBattlegroups = new ListGrid();
		myBattlegroups.setSelectionType(SelectionStyle.SINGLE);
        myBattlegroups.setSize("100%", "100%");
        myBattlegroups.setDataSource(new BattlegroupListDS(NoraBaseEditor.getLoginInfo().getAccount().getEmail()));
        myBattlegroups.setAutoFetchData(true);
        myBGlayout.addMember(myBattlegroups);
        myBattlegroupsTab.setPane(myBGlayout);
        tabSet.addTab(myBattlegroupsTab);
        
        Tab publicBattlegroupsTab = new Tab("Public Battlegroups");
        
        HLayout publicBGLayout = new HLayout();
        publicBGLayout.setMembersMargin(10);
        
        final ListGrid publicBattlegroups = new ListGrid();
		publicBattlegroups.setSelectionType(SelectionStyle.SINGLE);
        publicBattlegroups.setDataSource(new BattlegroupListDS(null));
        publicBattlegroups.setAutoFetchData(true);
        publicBGLayout.addMember(publicBattlegroups);
        publicBattlegroupsTab.setPane(publicBGLayout);
        tabSet.addTab(publicBattlegroupsTab);
        outerLayout.addMember(tabSet);

        // FIXME combine doubleclickhandlers with open click handler
        publicBattlegroups.addDoubleClickHandler(new DoubleClickHandler() {			
			@Override
			public void onDoubleClick(DoubleClickEvent event) {
				if (tabSet.getSelectedTab().equals(myBattlegroupsTab))
					bgts.openTab(myBattlegroups.getSelectedRecord().getAttribute("id"), myBattlegroups.getSelectedRecord().getAttribute("name"), NoraBaseEditor.getLoginInfo().getAccount().getBattlegroupGridPreference());
				else
					bgts.openTab(publicBattlegroups.getSelectedRecord().getAttribute("id"), publicBattlegroups.getSelectedRecord().getAttribute("name"), NoraBaseEditor.getLoginInfo().getAccount().getBattlegroupGridPreference());
			}
		});
        myBattlegroups.addDoubleClickHandler(new DoubleClickHandler() {			
			@Override
			public void onDoubleClick(DoubleClickEvent event) {
				if (tabSet.getSelectedTab().equals(myBattlegroupsTab))
					bgts.openTab(myBattlegroups.getSelectedRecord().getAttribute("id"), myBattlegroups.getSelectedRecord().getAttribute("name"), NoraBaseEditor.getLoginInfo().getAccount().getBattlegroupGridPreference());
				else
					bgts.openTab(publicBattlegroups.getSelectedRecord().getAttribute("id"), publicBattlegroups.getSelectedRecord().getAttribute("name"), NoraBaseEditor.getLoginInfo().getAccount().getBattlegroupGridPreference());
			}
		});

        
        HLayout buttonLayout = new HLayout();
        outerLayout.addMember(buttonLayout);
        buttonLayout.setAlign(Alignment.RIGHT);
        buttonLayout.setMembersMargin(10);
        
        Button btnOpen = new Button("Open");
        btnOpen.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (tabSet.getSelectedTab().equals(myBattlegroupsTab))
					bgts.openTab(myBattlegroups.getSelectedRecord().getAttribute("id"), myBattlegroups.getSelectedRecord().getAttribute("name"), NoraBaseEditor.getLoginInfo().getAccount().getBattlegroupGridPreference());
				else
					bgts.openTab(publicBattlegroups.getSelectedRecord().getAttribute("id"), publicBattlegroups.getSelectedRecord().getAttribute("name"), NoraBaseEditor.getLoginInfo().getAccount().getBattlegroupGridPreference());
				BattlegroupOpenDialog.this.destroy();
			}
		});
        buttonLayout.addMember(btnOpen);
        
        Button btnCancel = new Button("Cancel");
        btnCancel.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				BattlegroupOpenDialog.this.destroy();
			}
		});
        buttonLayout.addMember(btnCancel);
        addItem(outerLayout);
	}
}
